package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.FaceDb;
import com.xmy.cultivate.entity.Organization;
import com.xmy.cultivate.entity.StudentFaceMany;
import com.xmy.cultivate.entity.StudentFaceManyDetail;
import com.xmy.cultivate.mapper.StudentFaceManyDetailMapper;
import com.xmy.cultivate.respons.ResFaceData;
import com.xmy.cultivate.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.util.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 人脸采集 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-04-19
 */
@Service
public class StudentFaceManyDetailServiceImpl extends ServiceImpl<StudentFaceManyDetailMapper, StudentFaceManyDetail> implements IStudentFaceManyDetailService {

    @Autowired
    StudentFaceManyDetailMapper studentFaceManyDetailMapper;

    @Autowired
    IOrganizationService iOrganizationService;

    @Autowired
    IAliyunFaceService iAliyunFaceService;

    @Autowired
    IFaceDbService iFaceDbService;

    @Autowired
    IStudentFaceDataService iStudentFaceDataService;

    @Autowired
    IStudentFaceManyService iStudentFaceManyService;

    @Override
    public List<StudentFaceManyDetail> getStudentFaceManyDetailByStudentId(Long studentId, Long entityId){
        return studentFaceManyDetailMapper.getStudentFaceManyDetailByStudentId(studentId,entityId);
    }

    @Override
    public boolean delStudentFaceData(Long studentId,Integer faceType) throws Exception {
        FaceDb faceDb = iFaceDbService.getFaceDbOne(faceType);

        QueryWrapper<StudentFaceManyDetail> studentFaceManyDetailQueryWrapper = new QueryWrapper<>();
        studentFaceManyDetailQueryWrapper.eq("student_id",studentId);
        studentFaceManyDetailQueryWrapper.eq("face_type",faceType);
        List<StudentFaceManyDetail> studentFaceManyDetailList = this.list(studentFaceManyDetailQueryWrapper);
        for (StudentFaceManyDetail studentFaceManyDetailDel:studentFaceManyDetailList){
            Organization schoolAccount = iOrganizationService.getOneForAliAccount(studentFaceManyDetailDel.getAliAccount());
            boolean rs = iAliyunFaceService.delFaceData(schoolAccount.getId(), faceDb.getDbName(), studentFaceManyDetailDel.getFaceId());
            if (rs){
                this.removeById(studentFaceManyDetailDel.getId());
            }
        }
        return true;
    }

    /**
     * 只从阿里云删除数据
     * @param studentId
     * @return
     * @throws Exception
     */
    public boolean delStudentFaceDataForAliyun(Long studentId,Integer faceType) throws Exception {
        FaceDb faceDb = iFaceDbService.getFaceDbOne(faceType);

        QueryWrapper<StudentFaceManyDetail> studentFaceManyDetailQueryWrapper = new QueryWrapper<>();
        studentFaceManyDetailQueryWrapper.eq("student_id",studentId);
        List<StudentFaceManyDetail> studentFaceManyDetailList = this.list(studentFaceManyDetailQueryWrapper);
        for (StudentFaceManyDetail studentFaceManyDetailDel:studentFaceManyDetailList){
            Organization schoolAccount = iOrganizationService.getOneForAliAccount(studentFaceManyDetailDel.getAliAccount());
            boolean rs = iAliyunFaceService.delFaceData(schoolAccount.getId(), faceDb.getDbName(), studentFaceManyDetailDel.getFaceId());
        }
        return true;
    }

    @Override
    public boolean updateStudentFaceData(Long studentId,Integer faceType) throws Exception {
        try{
            this.delStudentFaceDataForAliyun(studentId,faceType);
            QueryWrapper<StudentFaceManyDetail> studentFaceManyDetailQueryWrapper = new QueryWrapper<>();
            studentFaceManyDetailQueryWrapper.eq("student_id",studentId);
            studentFaceManyDetailQueryWrapper.eq("face_type",faceType);
            List<StudentFaceManyDetail> studentFaceManyDetailList = this.list(studentFaceManyDetailQueryWrapper);
            for (StudentFaceManyDetail studentFaceManyDetail:studentFaceManyDetailList){
                if (!studentFaceManyDetail.getStudentId().equals(studentFaceManyDetail.getEntityId())){
                    QueryWrapper<Organization> organizationQueryWrapper = new QueryWrapper<>();
                    organizationQueryWrapper.eq("type",1);
                    organizationQueryWrapper.eq("status",1);
                    organizationQueryWrapper.eq("ali_account",studentFaceManyDetail.getAliAccount());
                    organizationQueryWrapper.last("limit 1");
                    Organization organization = iOrganizationService.getOne(organizationQueryWrapper);
                    if (organization == null){
                        studentFaceManyDetail.setPushMqType(3);//异常
                        studentFaceManyDetail.setRemark("人脸帐号校区不存在");
                        this.updateById(studentFaceManyDetail);
                        return false;
                    }
                    ResFaceData resFaceData = iStudentFaceDataService.setDataForMany(organization.getId(),studentFaceManyDetail,studentFaceManyDetail.getFaceType());
                    if (resFaceData.getCode().equals(Code.OK)){
                        studentFaceManyDetail.setPushMqType(2);
                        studentFaceManyDetail.setEntityId(resFaceData.getEntityId());
                        studentFaceManyDetail.setFaceId(resFaceData.getFaceId());
                        studentFaceManyDetail.setQualitieScore(resFaceData.getQualitieScore());
                        studentFaceManyDetail.setRemark("");
                        this.updateById(studentFaceManyDetail);

                        QueryWrapper<StudentFaceMany> studentFaceManyQueryWrapper = new QueryWrapper<>();
                        studentFaceManyQueryWrapper.eq("ali_account",studentFaceManyDetail.getAliAccount());
                        studentFaceManyQueryWrapper.eq("student_id",studentFaceManyDetail.getStudentId());
                        studentFaceManyQueryWrapper.eq("entity_id",resFaceData.getEntityId());
                        Long count = iStudentFaceManyService.count(studentFaceManyQueryWrapper);
                        //已经存在不添加，不存在就添加
                        if (count>0){
                            return true;
                        }
                        StudentFaceMany studentFaceMany = new StudentFaceMany();
                        studentFaceMany.setStudentId(resFaceData.getStudentId());
                        studentFaceMany.setFaceId(resFaceData.getFaceId());
                        studentFaceMany.setAliAccount(studentFaceManyDetail.getAliAccount());
                        studentFaceMany.setEntityId(resFaceData.getEntityId());
                        studentFaceMany.setOssImage(studentFaceManyDetail.getOssImage());
                        studentFaceMany.setQualitieScore(resFaceData.getQualitieScore());
                        iStudentFaceManyService.save(studentFaceMany);
                        return true;
                    }else {
                        studentFaceManyDetail.setRemark(resFaceData.getMsg());
                        this.updateById(studentFaceManyDetail);
                        return false;
                    }
                }
            }
        }catch (Exception e){
        }
        return true;
    }
}
